package dao

import (
	"go-weixin/app/diary/model"
	"go-weixin/common/chendb"
)

type DiaryDao struct {
	engine chendb.ChenDb
}

func NewDiaryDao(engine chendb.ChenDb) *DiaryDao {
	return &DiaryDao{
		engine: engine,
	}
}

func (d DiaryDao) DiaryAdd(userId int64, title string, content string) (int64, error) {
	return d.engine.Insert(&model.DiaryModel{
		UserId:  userId,
		Title:   title,
		Content: content,
	})
}

func (d DiaryDao) DiaryUpdate(id int64, userId int64, title string, content string) (int64, error) {
	return d.engine.ID(id).Update(&model.DiaryModel{
		UserId:  userId,
		Title:   title,
		Content: content,
	})
}

func (d DiaryDao) DiaryDeleteById(id int64) (int64, error) {
	return d.engine.ID(id).Delete(&model.DiaryModel{})
}

func (d DiaryDao) DiaryList(limit int64, offset int64, diaryList *[]model.DiaryModel) error {
	return d.engine.Limit(int(limit), int(offset)).Find(diaryList)
}

func (d DiaryDao) DiaryGetById(id int64, diary *model.DiaryModel) (bool, error) {
	return d.engine.ID(id).Get(diary)
}

func (d DiaryDao) DiaryLikeByTitle(title string, limit int, offset int, diaryList *[]model.DiaryModel) error {
	return d.engine.Where("title like ?", "%"+title+"%").Limit(limit, offset).Find(diaryList)
}
